<?php

declare(strict_types=1);

namespace app\api\controller;

use app\common\model\Coupon as CouponModel;
use app\common\model\Usercoupon;
use app\common\model\CouponProduct;
use app\common\model\Order;
use app\common\tool\Result;
use think\exception\ValidateException;

class Coupon extends Base
{
    //优惠券列表
    public function getList()
    {
        $param = $this->request->param();
        $map[] = ['is_del', '=', 0];
        $map[] = ['types', '=', 1];
        $map[] = ['status', '=', 1];
        $map[] = ['level_ids', 'like', '%' . $this->user->level_id . '%'];
        $sort = ['key' => 'sort', 'value' => 'asc'];

        $list = CouponModel::getPageList($map, '*', $param['page'], 20, false, '', '', $sort);
        foreach ($list['data'] as $k => $v) {
            $count = Usercoupon::where(['uid' => $this->user->id, 'coupon_id' => $v['id']])->count();
            $v['lq'] = $count > 0 ? 1 : 0;
            $list['data'][$k] = $v;
        }
        return Result::success('获取成功！', $list);
    }

    //下单优惠券列表列表
    public function mygetList()
    {
        $param = $this->request->param();
        $usermap[] = ['end_time', '>=', time()];
        $usermap[] = ['uid', '=', $this->user->id];
        $usermap[] = ['order_id', '<>', $param['order_id']];
        $usermap[] = ['is_del', '=', 0];
        $ids = Usercoupon::where($usermap)->column('coupon_id');
        //print('<pre>');print_r($ids);die;
        $order = Order::where(['id' => $param['order_id'], 'is_del' => 0])->value('real_amount');

        $map[] = ['is_del', '=', 0];
        $map[] = ['status', '=', 1];
        $map[] = ['id', 'not in', $ids];
        $listpt = CouponModel::where($map)->where(['types' => 1])->order('sort asc')->select()->toArray();
        if (!empty($order)) {
            $map2[] = ['term_money', '<=', $order];
            $map2[] = ['types', '=', 2];
            $map2[] = ['status', '=', 1];
            $map2[] = ['is_del', '=', 0];
            $listorder = CouponModel::where($map2)->order('term_money desc')->select()->toArray();
            if (!empty($listorder)) {
                foreach ($listorder as $k => $v) {
                    if ($v['receive'] == 1 && in_array($v['id'], $ids)) {
                        unset($listorder[$k]);
                    }
                }
                $listorders = $listorder;
            } else {
                $listorders = [];
            }
        } else {
            $listorders = [];
        }
        $list = array_merge($listorders, $listpt);

        return Result::success('获取成功！', $list);
    }


    //领取优惠券
    public function couponAdd()
    {
        try {
            $param = $this->request->post();
            return Result::fastReturn(Usercoupon::couponAdd($param, $this->user));
        } catch (ValidateException $e) {
            return Result::fail($e->getError());
        } catch (\Exception $e) {
            return Result::fail($e->getMessage());
        }
    }
    //全部领取优惠券
    public function couponAllAdd()
    {
        try {
            $param = $this->request->post();
            return Result::fastReturn(Usercoupon::couponAllAdd($this->user));
        } catch (ValidateException $e) {
            return Result::fail($e->getError());
        } catch (\Exception $e) {
            return Result::fail($e->getMessage());
        }
    }
    //我的优惠券
    public function getMyList()
    {
        $param = $this->request->param();
        if ($param['type'] == 0) {
            $map[] = ['status', '=', 0];
            $map[] = ['end_time', '>=', time()];
        } else if ($param['type'] == 1) {
            $map[] = ['end_time', '>=', time()];
            $map[] = ['status', '=', 1];
        } else {
            $map[] = ['end_time', '<', time()];
            $map[] = ['status', '=', 0];
        }
        $map[] = ['uid', '=', $this->user->id];
        $map[] = ['is_del', '=', 0];
        $sort = ['key' => 'end_time', 'value' => 'asc'];
        $list = Usercoupon::getPageList($map, '*', $param['page'], 20, false, '', '', $sort);
        foreach ($list['data'] as $k => $v) {
            $v['end_times'] = date('Y-m-d H:i:s', $v['end_time']);
            $list['data'][$k] = $v;
        }
        return Result::success('获取成功！', $list);
    }
    //获取优惠券
    public function myMoupon()
    {
        $param = $this->request->param();
        $mapdata = json_decode(htmlspecialchars_decode($param['mapdata']), true);
        // print('<pre>');print_r($mapdata);die;
        $map[] = ['money', '<=', $param['money']];
        $map[] = ['end_time', '>=', time()];
        $map[] = ['status', '=', 0];
        $map[] = ['is_del', '=', 0];
        $map[] = ['uid', '=', $this->user->id];
        $ids = [];
        foreach ($mapdata as $kk => $vv) {
            $count = $vv['price'] * $vv['num'];
            if ($vv['seckill_id'] == 0) {
                $couponlist = CouponProduct::where(['product_id' => $vv['id'], 'is_del' => 0])->distinct(true)->group('coupon_id')->column('coupon_id');
                $maps[] = ['money', '<=', $count];
                $coupon_id[] = CouponModel::where($maps)->whereIn('id', $couponlist)->column('id');
            } else {
                $coupon_id = [];
            }
        }
        foreach ($coupon_id as $ky => $vy) {
            if (!empty($vy)) {
                foreach ($vy as $kk => $v) {
                    if (!in_array($v, $ids)) {
                        $ids[] = $v;
                    }
                }
            }
        }
        // print('<pre>');
        // print_r($ids);
        // die;

        $map[] = ['coupon_id', 'IN', $ids];
        $list = Usercoupon::where($map)->order('end_time asc')->select();
        foreach ($list as $k => $v) {
            if ($param['coupon_id'] != 0 && $v['id'] == $param['coupon_id']) {
                $v['check'] = 1;
            } else {
                $v['check'] = 0;
            }
            $list[$k] = $v;
        }
        return Result::success('获取成功！', $list);
    }
}
